package com.source.hotelbath.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

/**
 * support4 包
 * dialogfragment 如果使用异步弹出对话框，当点了home键会爆出saveinstancestate bug，程序崩溃
 * 如果强制改dialogfragment源码 commitAllowingStateLoss 没有了onsaveinstancestate，
 * 和普通dialog没啥区别不大，只是可以再activity被回收后能重建，但是如果有listener
 * 的对话框，当前页面被回收了，再次启动的时候回到这个对话框如果有listener 没了，当页面有
 * 多个fragment来回切换，用dialogfragment 会导致fragmenttransaction缓存问题，有null的fragment
 * fragmentmanager中可以清理但是fragmenttransaction没法清理
 * 如果切换账号进入activity 清理所有的fragment再重新添加，这时候fragmenttransaction 数组下表越界
 * 安全起见用Dialog
 * BOTTOM,TOP的计算有些出入，但是一定可以显示到屏幕的地步和头部（没啥影响）
 */
public abstract class BaseDialog extends Dialog {


    private boolean mIsFullScreen=false;
    private Position mPosition= Position.DEFAULT;
    protected Context mContext;
    public enum Position{           //内容显示的位置，默认gravity是中间
        DEFAULT,BOTTOM,TOP;
    }

    public BaseDialog(Context context) {
        super(context);
        mContext=context;
    }
    public BaseDialog(Context context, int theme) {
        super(context,theme);
        mContext=context;
    }

    public BaseDialog(Context context, int theme, boolean isFullScreen, Position position) {
        super(context, theme);
        mIsFullScreen=isFullScreen;
        mPosition=position;
        mContext=context;


        setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent arg2) {
                // TODO Auto-generated method stub
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    dismiss();
                    return true;
                }
                return false;
            }
        });
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                BaseDialog.this.onDismiss();
            }
        });
    }




    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        if(mIsFullScreen){
            DisplayMetrics dm = new DisplayMetrics();
            ((Activity)mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
            int screenwitdh = dm.widthPixels;
            int screenHeight = dm.heightPixels;
            Window window = this.getWindow();
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = screenwitdh;
            window.setAttributes(params);
        }
        if(mPosition== Position.BOTTOM){
            DisplayMetrics dm = new DisplayMetrics();
            ((Activity)mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
            int screenHeight = dm.heightPixels;
            Window window = this.getWindow();
            WindowManager.LayoutParams params = window.getAttributes();
            params.x = 0;//设置x相对中间的偏移
            params.y = (screenHeight) / 2;//设置y相对中间的偏移
            window.setAttributes(params);
        }else if(mPosition== Position.TOP){
            DisplayMetrics dm = new DisplayMetrics();
            ((Activity)mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
            int screenHeight = dm.heightPixels;
            Window window = this.getWindow();
            WindowManager.LayoutParams params = window.getAttributes();
            params.x = 0;//设置x坐标
            params.y = -(screenHeight) / 2;//设置y坐标
            window.setAttributes(params);
        }
    }

    public void onDismiss(){};
}
